package com.holly.common.utils;

import android.support.annotation.NonNull;

import com.google.gson.Gson;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 集合 等 toString
 *
 * @author Joey Huang（QQ:273179370）
 * @created 2016/6/10
 */
public class ToStrUtils {

    private static Gson gson;

    public static String toString(Map map) {
        if (map == null || map.isEmpty()) {
            return "";
        }
        String str = "{";
        Set keySet = map.keySet();
        for (Object key : keySet) {
            if (key instanceof String) {
                str = str + key + "=" + map.get(key) + "  ";
            }
        }
        str = str.substring(0, str.length() - 2);
        str += "}";
        return str;
    }


    public static String toString(String[] array) {
        String str = "[";
        if (array == null || array.length == 0) {
            return str + "]";
        }
        for (String s : array) {
            str = str + s + ",";
        }
        str = str.substring(0, str.length() - 1) + "]";
        return str;
    }

    public static String toString(List list) {
        String str = "[";
        if (list == null || list.size() == 0) {
            return str + "]";
        }
        for (Object bean : list) {
            if (bean != null) {
                str = str + bean.toString() + ",";
            } else {
                str = str + "" + ",";
            }
        }
        str = str.substring(0, str.length() - 1) + "]";
        return str;
    }

    @NonNull
    public static String toString(String str) {
        if (str == null || str.trim().length() == 0) {
            return "";
        } else {
            return str;
        }
    }

    public static String toUrl(Map<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return "";
        }
        String str = "?";
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            str = str + key + "=" + map.get(key) + "&";
        }
        str = str.substring(0, str.length() - 1);
        return str;
    }

    public static String toNonNullString(Object object) {
        if (object == null) {
            return "";
        } else if (object instanceof String) {
            return (String) object;
        } else {
            return object.toString();
        }
    }

    public static String toNonNullString(String string) {
        if (string == null) {
            return "";
        } else {
            return string;
        }
    }

    public static String toNonNullString(String string, String defaultString) {
        if (string == null || string.isEmpty()) {
            if (defaultString == null) {
                return "";
            } else {
                return defaultString;
            }
        } else {
            return string;
        }
    }

    public static String toJsonString(Object o) {
        if (gson == null) {
            gson = new Gson();
        }
        if (o == null) {
            return "{}";
        }
        return gson.toJson(o);
    }
}
